<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>vuex核心概述</title>
  <link rel="stylesheet" href="../css/base.css">
  <script src="../js/vue.js"></script>
  <script src="../js/vue_router.js"></script>
  <script src="../js/vuex.js"></script>
  <style>
    .cls {
      width: 100%;
      height: 5rem;
      border: 0.2rem solid red;
    }
  </style>
</head>
<!-- 
  Vuex的核心概述
    State:提供唯一个公共数据源，所有共享的数据都要统一放到Store的state中进行存储
    Mutation
    Action
    Getter
 -->

<!-- 
  创建store数据源，提供了一个公共的数据
    const store = new Vuex.Store({
      state: {
        count: 1
      }
    });
    
  组件访问State中的数据的第一种方式
    this.$store.state.全局数据名称
 -->

<body>
  <div id="app">
    <h3>当前最新的count的值:{{this.$store.state.count}}</h3>
    <input type="button" value="增加了吧" @click="add">
    <cls />
  </div>
</body>
<script>
  "use strict";
  const store = new Vuex.Store({
    state: {
      count: 0
    }
  });

  const cls = {
    template: `
      <div class="cls">
        <h1 style="color:red;">{{this.$store.state.count}}</h1>
        <input type="button" value="减少了吧" @click="inset">
      </div>`,
    methods: {
      inset() {
        this.$store.state.count--;
      }
    }
  };

  const vm = new Vue({
    el: "#app",
    data() {
      return {

      };
    },
    methods: {
      add() {
        this.$store.state.count++;
      }
    },
    components: {
      cls
    },
    store
  });
</script>

</html>